import Vue from 'vue'
import Vuex from 'vuex'
import api from './api'

import center from './center'
import cases from './cases'
import identity from './identity'
import lawyer from './lawyer'
import message from './message'
import VuexPersistence from 'vuex-persist'
Vue.use(Vuex)
const vuexLocal = new VuexPersistence({
    storage: window.localStorage
})

const store = new Vuex.Store({
	modules: {api,cases,center,identity,lawyer,message},
    state: {
		userinfo:{},//用户信息
		vipinfo:{},//vip信息
		
		region:[],//地区信息
		lawFirm:[],//律所
		config:{},//配置信息
		newsObj:[],//客服消息
		imInfo:{},//客服登录信息
		locations:{region_name:'',id:''},//定位信息
		user_id:'',//用户信息
		token:'',//登录信息
        level: 1, // 1 用户 2律师 3律所
        vip: 1,// 是否是vip
		levels:0,
		files:{},
		imLogin:false,
		version:''
    },
    mutations: {
		//保存用户信息
		setUser(state,data){
		    window.sessionStorage.setItem('userinfo',JSON.stringify(data));
			state.user_id = data.user_id;
			if(data.type==0){
				state.level = 1
			}if(data.type==1){
				state.level = 1
			}else if(data.type==2){
				state.level = 3
			}else if(data.type==3){
				state.level = 2
			}else if(data.type==8){
				state.level = 3
			}
			state.vip = data.vip;
		    state.userinfo = data;
		    
		},
		//保存VIP信息
		setVip(state,data){
		    window.sessionStorage.setItem('vipDetail',JSON.stringify(data));
		    state.vipinfo = data;
		    
		},
		//保存版本
		setVersion(state,data){
			state.version = data;
		},
		//保存地区
		setRegion(state,data){
		    window.sessionStorage.setItem('region',JSON.stringify(data));
		    state.region = data;
			state.region.forEach((item)=>{
				if(item.region_name==data){
					state.locations = item
					return
				}
				item.children.forEach(item1=>{
					if(item1.region_name==data){
						state.locations = item1
						return
					}
				})
			})
		},
		//保存配置
		setConfig(state,data){
		    window.sessionStorage.setItem('config',JSON.stringify(data));
		    state.config = data;
		},
		//保存配置
		setLocation(state,data){
			window.localStorage.setItem('location', JSON.stringify(data));
			state.locations = {region_name:data,id:''};
			state.region.forEach((item)=>{
				if(item.region_name==data){
					state.locations = item
					return
				}
				item.children.forEach(item1=>{
					if(item1.region_name==data){
						state.locations = item1
						return
					}
				})
			})
		},
		//保存律所
		setLawFirm(state,data){
			window.localStorage.setItem('lawFirm', JSON.stringify(data));
		    state.lawFirm = data;
		},
		//保存客服消息
		setServe(state,data){
			window.localStorage.setItem('newsObj', JSON.stringify(data));
		    state.newsObj = data;
		},
		//保存客服登录信息
		setImLogin(state,data){
			window.localStorage.setItem('imInfo', JSON.stringify(data));
			
		    state.imInfo = data;
		},
		/**
		 * 设置token
		 * @param state
		 * @param data
		 */
		setToken(state, data) {
			state.user_id = data.user_id;
			state.vip = data.vip;
		    state.token = data.access_token;
			
		},
		/**
		 * 设置token
		 * @param state
		 * @param data
		 */
		setLevels(state, data) {
		    state.levels = data;
		},
		/**
		 * 设置file
		 * @param state
		 * @param data
		 */
		setFiles(state, data) {
		    state.files = data;
		},
		/**
		 * 设置IMLogin
		 * @param state
		 * @param data
		 */
		setIMLogin(state, data) {
		    state.imLogin = data;
		},
	},
    actions: {},
    plugins: [vuexLocal.plugin]
})
export default store